package com.ruoyi.web.core.config.mqtt;

import com.ruoyi.system.config.SystemConfig;
import com.ruoyi.system.domain.SysDevice;
import com.ruoyi.system.service.ICommonService;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chmj
 * @version V1.0
 * @Package com.ruoyi.web.core.config.mqtt
 * @Copyright 2022/10/10 19:11 汇博医疗股份有限公司
 */
@Component
public class MqttReconnectCallback implements MqttCallbackExtended {

    @Autowired
    private ICommonService commonService;

    public static MqttReconnectCallback mqttReconnectCallback;

    @PostConstruct
    public void init() {
        mqttReconnectCallback = this;
        mqttReconnectCallback.commonService = this.commonService;
    }

    @Override
    public void connectComplete(boolean reconnect, String serverURI) {
        Map<String,Object> message = new HashMap<>();
        message.put("version","1.0");
        message.put("method","set");
        Map<String,Object> params = new HashMap<>();
        params.put("reportMode","0");
        message.put("params",params);
        List<Integer> deviceTypeList = new ArrayList<>();
        deviceTypeList.add(4);
        deviceTypeList.add(5);
        List<SysDevice> deviceList = commonService.queryDeviceByType(deviceTypeList);
        for (SysDevice device : deviceList){
            SystemConfig.heartNumMap.put(device.getDeviceCode(),0);
            MqttUtil.send("/Radar60SP/"+ device.getDeviceCode() +"/sys/property/set",message);
            MqttUtil.subscribe("/Radar60SP/"+ device.getDeviceCode() +"/sys/property/post",new MessageListener());
        }
//        MqttUtil.subscribe("/Radar60SP/a4cf12c1f5f4/sys/property/post",new MessageListener());
    }

    @Override
    public void connectionLost(Throwable cause) {

    }

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {

    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {

    }
}
